/*
#include <Module/IO_Driver.h>
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-04     Administrator       the first version
 */


#include "IO_Driver.h"

#include <rtthread.h>
#include <cstring>
#include "ulog.h"

#include "GlobalVar.h"
#include "DevManager.h"
#include "pcf8575.h"


//led
#ifndef RUN_LED_PIN_NUM
    #define RUN_LED_PIN_NUM            31  /* PB15 */
    #define ERR_LED_PIN_NUM            30  /* PF14 */
#endif

void Run_LED_ON(void *args)
{
    rt_pin_write(RUN_LED_PIN_NUM, PIN_LOW);
}
void Run_LED_OFF(void *args)
{
    rt_pin_write(RUN_LED_PIN_NUM, PIN_LOW);
}
void Err_LED_ON(void *args){
    rt_pin_write(RUN_LED_PIN_NUM, PIN_LOW);
}
void Err_LED_OFF(void *args){
    rt_pin_write(RUN_LED_PIN_NUM, PIN_LOW);
}

void IO_Init()
{
    //run led
    rt_pin_mode(RUN_LED_PIN_NUM, PIN_MODE_OUTPUT);
    rt_pin_write(RUN_LED_PIN_NUM, PIN_HIGH);
    //err led
    rt_pin_mode(ERR_LED_PIN_NUM, PIN_MODE_OUTPUT);
    rt_pin_write(ERR_LED_PIN_NUM, PIN_HIGH);
}

/*******
 * led task.
 * @param parameter
 */
void LED_Task(void *parameter)
{
    while(1)
    {
        Run_LED_OFF(NULL);
        DelayMs(500);
        Run_LED_ON(NULL);
        DelayMs(500);
    }
}
//led end

//io driver
void IO_driver_Task(void *parameter)
{
    uint8_t buffer[8];
    uint8_t valve;
    uint8_t driver_num;
    static uint32_t times = 0;
    pcf8575_device_t io_dev_handle;
    io_dev_handle = io_ctrl_init();
    while(1)
    {
        rt_mq_recv(task.IO_driver.task_msg_queue, buffer, 8, RT_WAITING_FOREVER);
        LOG_I("io crtl have run %d times",++times);
        driver_num = QueryOfDevDriverNum(buffer[0]);
        switch (buffer[1])
        {
            case 4:
                valve = buffer[2];
                io_ctrl(io_dev_handle,driver_num,valve);
                break;
            default:
                break;
        }
    }
    io_ctrl_deinit(io_dev_handle);
}

//end
